const utils = require("../../../utils/utils");

// pages/my/feedback/feedback.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chooseImgArray:[
    ],
    serviceImgArray:[],
    mobile:'',
    remark:'',
    firstOpen:0
  },
    //删除图片
    deleteImg(e){
      var index = e.currentTarget.dataset.index;
      var imgArray = this.data.chooseImgArray;
      imgArray.splice(index,1)
      this.setData({
        chooseImgArray:imgArray
      })
    },
    //选择图片
    chooseImgBtn(){
      var that = this;
      wx.showActionSheet({
        itemList: ['相册', '拍照', '从QQ会话选择'],
        success(res) {
          if (res.tapIndex == 0) {
            that.getPhoto('album');
          }
          if (res.tapIndex == 1) {
            that.getPhoto('camera');
          }
          if (res.tapIndex == 2) {
            that.getPhoto1();
          }
        },
        fail(res) {
          
        }
      })
    //   wx.chooseImage({
    //     count:3,
    //     sizeType: ['compressed'],
    //     sourceType: ['album', 'camera'],
    //     success (res) {
    //       var array = that.data.chooseImgArray;
    //       // tempFilePath可以作为img标签的src属性显示图片
    //       for(var i=0;i<res.tempFilePaths.length;i++){
    //         array.push(res.tempFilePaths[i])
    //       }
    //       that.setData({
    //         chooseImgArray:array
    //       })
    //     }
    //   })
    },
      //拍照调取相册
  getPhoto(type) {
    var that = this;
    wx.chooseImage({
      count:9,
      sizeType: ['original'],
      sourceType: [type],
      success(res) {
            var array = that.data.chooseImgArray;
            // tempFilePath可以作为img标签的src属性显示图片
            for(var i=0;i<res.tempFilePaths.length;i++){
                var obj = {};
                obj.file = res.tempFilePaths[i];
                obj.type = 'image'
                array.push(obj)
            }
            that.setData({
                chooseImgArray:array
            })
      }
    })
  },
  getPhoto1() {
    var that = this;
    wx.chooseMessageFile({
      count: 9,
      success(res) {
         var array = that.data.chooseImgArray;
          for (var i = 0; i < res.tempFiles.length; i++) {
            var obj = {};
            obj.file = res.tempFiles[i].path;
            obj.type =  res.tempFiles[i].type;
            array.push(obj)
          }
          that.setData({
            chooseImgArray:array
        })
      }
    })
  },
    inputtitle(e){
      this.setData({
        title:e.detail.value
      })
    },
    inputmobile(e){
      this.setData({
        mobile:e.detail.value
      })
    },
    inputremark(e){
      this.setData({
        remark:e.detail.value
      })
    },
    btnConfirm(){
      var mobile = this.data.mobile;
      var remark = this.data.remark;
      var title = this.data.title;
      var imgArr = this.data.chooseImgArray;
      if (!utils.is_define(mobile)) {
        utils.toast("请输入手机号");
        return false;
      }
      if (!utils.is_mobile(mobile)) {
        utils.toast("请输入正确的手机号");
        return false;
      }
      if (!utils.is_define(title)) {
        utils.toast("请用一句话您遇到的问题");
        return false;
      }
      if (!utils.is_define(remark)) {
        utils.toast("请仔细描述您遇到的问题");
        return false;
      }
      wx.showLoading()
      //开始上传图片
      if(imgArr.length==0){
        //直接提交
        this.subTo();
      }else{
        this.uploadFileSome(imgArr[0].file,0);
      }
    },
    uploadFileSome(url,index){
      var that = this;
      var list = that.data.chooseImgArray;
      var imgs = that.data.serviceImgArray;
      utils.uploadFileMin(url,function(res){
        var data = JSON.parse(res.data).data
          imgs.push(data.fileUrl)
          that.setData({
            serviceImgArray:imgs
          })
          if(index==list.length-1){
            that.subTo();
          }else{
            that.uploadFileSome(list[index+1].file,index+1)
          }
      })
    },
    subTo(){
      var that = this;
      var mobile = that.data.mobile;
      var remark = that.data.remark;
      var title = that.data.title;
      var imgs = that.data.serviceImgArray;
      var str = '';
      if(imgs.length>0){
          str = imgs.join(',')
      }
      wx.hideLoading();
      wx.navigateTo({
        url: '../customOrderPay/customOrderPay?taskNo=&price='+that.data.price+'&title='+title+'&remark='+remark+'&tel='+mobile+'&attach='+str
      })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 定制轮播图
    this.getAdv();
    //定制价格
    this.getCusPrice();
  },
  getCusPrice(){
    var that = this;
    utils.request('customOrder/getPrice', {}, function (res) {
        that.setData({
            price:res.data.data.price
        })
    })
  },
  getAdv(){
    var that = this;
    utils.request('ad/lists/63706488', {}, function (res) {
        that.setData({
            adv:res.data.data
        })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (that.data.firstOpen == 1) {
      // 定制轮播图
    this.getAdv();
    //定制价格
    this.getCusPrice();
    } else {
      that.setData({
        firstOpen: 1
      })
    }
  },
  goToLink(e) {
    var url = e.currentTarget.dataset.link;
    var id = e.currentTarget.dataset.id;
    var title = e.currentTarget.dataset.title;
    var content = e.currentTarget.dataset.content;
    if (utils.is_define(url)) {
        if (url.indexOf('pages') != -1 && url.indexOf('mjyy') == -1) {
            wx.navigateTo({
                url: url,
            })
            return false;
        }
        if (url.indexOf('pages') != -1 && url.indexOf('mjyy') != -1) {
            wx.navigateToMiniProgram({
                appId: 'wxd8a7d886dc3c37d5',
                path: url,
                envVersion: 'release',
                success(res) {
                    // 打开成功
                }
            })
            return false;
        }
        if (url.indexOf('https') != -1) {
            wx.navigateTo({
                url: '/pages/my/agreement/agreement?url=' + url
            })
            return false;
        }
        if(content){
            //信息详情
            wx.navigateTo({
              url: '/pages/my/pictureAdv/pictureAdv?id='+id+'&name='+title
            })
        }
    }
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})